﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Pages.ChildWindows;

namespace Rio.Framework.Pages.CriteriaControls
{
    public partial class ListGroupIndexControl 
    {
        private string m_szSelectedGroups;
        public List<Group> SelectedGroups { set; get; }

        public override sealed string InputValue
        {
            get
            {
                return String.IsNullOrWhiteSpace(m_szSelectedGroups) ? String.Empty : m_szSelectedGroups;
            }
            set
            {
                m_szSelectedGroups = value;
                if (m_szSelectedGroups != null)
                {
                    ToTextBox.Text = m_szSelectedGroups;
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ListGroupIndexControl"/> class.
        /// </summary>
        public ListGroupIndexControl()
        {
            InitializeComponent();

            _originalBorderBrush = this.ToTextBox.BorderBrush as SolidColorBrush;
        }


        /// <summary>
        /// Create an instance of the control
        /// </summary>
        /// <param name="p_oindex"><see cref="Index"/> instance</param>
        /// <param name="P_szValue">Default value</param>
        /// <param name="P_SetDefaultValue">Use default rio value</param>
        public ListGroupIndexControl(Index p_oindex, string P_szValue = null, bool P_SetDefaultValue = false)
            :this()
        {
            CurrentIndex = p_oindex;
            LabelControl.Text = CurrentIndex.IndexLabel;

            //if a user is selected at the start
            if (P_szValue != null && !string.Empty.Equals(P_szValue))
            {
                InputValue = P_szValue;
            }
            else
            {
                if (P_SetDefaultValue)
                    this.SetDefaultValue();
            }

            //Mandatory indication
            LabelMandatory.Visibility = CurrentIndex.IsMandatory ? Visibility.Visible : Visibility.Collapsed;
            FilterButton.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Display filter window with a list of values input
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonInput_Click(object sender, RoutedEventArgs e)
        {
            OnFilterClick(new Events.FilterClickEventArgs(this.CurrentIndex));
        }

        /// <summary>
        /// Group filter window event closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void L_oGroupFilterWindow_Closed(object sender, EventArgs e)
        {
            GroupFilterWindow L_oGroupFilterWindow = (GroupFilterWindow)sender;
            SelectedGroups = L_oGroupFilterWindow.m_szSelectedItems;

            //if a value has been selected
            if (L_oGroupFilterWindow.DialogResult == true)
            {
                try
                {
                    this.InputValue = L_oGroupFilterWindow.InputValue;
                    OnValueValidated(EventArgs.Empty);

                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the buttonDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            OnRemoveControlClick(new EventArgs());
        }

        /// <summary>
        /// 
        /// </summary>
        public override void Clear()
        {
            ToTextBox.Text = String.Empty;
        }

        /// <summary>
        /// Throw validation event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListboxInput_LostFocus(object sender, RoutedEventArgs e)
        {
            OnValueValidated(EventArgs.Empty);
        }

        /// <summary>
        /// Used to set the default value
        /// </summary>
        public override sealed void SetDefaultValue()
        {
            if (!string.IsNullOrEmpty(this.CurrentIndex.DefaultValue))
            {
                ToTextBox.Text = this.CurrentIndex.DefaultValue;
            }
        }

        /// <summary>
        /// Used to set border color of input control
        /// </summary>
        public override void ChangeBorderColor(Color color)
        {
            this.ToTextBox.BorderBrush = new SolidColorBrush(color);
        }

        /// <summary>
        /// Reverts the color of the border.
        /// </summary>
        public override void RevertBorderColor()
        {
            this.ToTextBox.BorderBrush = _originalBorderBrush;
        }

        /// <summary>
        /// Set the max lenght of the control
        /// </summary>
        public override void SetMaxLength()
        {
        }

        /// <summary>
        /// Hide mandatory label
        /// </summary>
        public override void DisableMandatoryLabel()
        {
            this.LabelMandatory.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Hides Index label name
        /// </summary>
        public override void DisableIndexLabel()
        {
            this.LabelControl.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Activate Removing feature
        /// </summary>
        public override void ActiveRemove()
        {
            this.DeleteButton.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Hides filter button
        /// </summary>
        public override void DisableFilter()
        {
            this.FilterButton.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Handles the KeyUp event of the ToTextBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs" /> instance containing the event data.</param>
        private void ToTextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Space || e.Key == System.Windows.Input.Key.Enter)
            {
                OpenGroupList();
            }
        }

        /// <summary>
        /// Handles the MouseLeftButtonDown event of the ToTextBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs" /> instance containing the event data.</param>
        private void ToTextBox_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OpenGroupList();
        }

        /// <summary>
        /// Opens the group list.
        /// </summary>
        private void OpenGroupList()
        {
            GroupFilterWindow l_oGroupFilterWindow = new GroupFilterWindow(this.ToTextBox.Text);
            l_oGroupFilterWindow.Closed += L_oGroupFilterWindow_Closed;
            l_oGroupFilterWindow.ShowDialog();
        }
    }

}
